<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/29
 * Time: 10:39
 */

namespace backend\controllers;

use backend\models\Baseconfigs;
use backend\models\Basemaster;
use backend\models\Basemessages;
use backend\models\Baserole;
use backend\models\Baserolemaster;
use backend\models\Baseprivilege;
use yii;
use yii\web\Controller;
use yii\web\Response;

class CommonController extends Controller
{
    public $enableCsrfValidation = false;

    public $roleId;//角色id

    public $userId;//用户id

    public $userName;//用户名

    public $roleName;//角色名称

    public $userType;

    public function init()
    {
        $this->userId = yii::$app->user->getId();
        if ($this->userId){
            $this->roleId = Baserolemaster::findOne(['MasterId' => $this->userId])->RoleId;
            $master = Basemaster::findOne(['MasterId' => $this->userId]);
            $role = Baserole::findOne(['Id' => $this->roleId]);
            $this->userName = $master->UserName;
            $this->roleName = $role->RoleName;
            $this->userType = $master->UserType;
            $config = Baseconfigs::getConfig();
            yii::$app->view->params['domain'] = $config['domain']->CfgVal;
            yii::$app->view->params['company'] = $config['company']->CfgVal;
            yii::$app->view->params['Photo'] = $role->HeadUrl;
            yii::$app->view->params['UserName'] = $this->userName;
            yii::$app->view->params['RoleName'] = $this->userName . "[ " . $this->roleName . " ]";
            yii::$app->view->params['LoginIp'] = $master->LoginIP;
            yii::$app->view->params['LoginDate'] = $master->LoginDate;
            yii::$app->view->params['userType'] = $master->UserType;
            //同一个系统，区分两套角色的操作 ----0:管理系统--------1:代理系统
            yii::$app->view->params['userType'] = $this->userType;
            //消息提示
            $message_count = Basemessages::find()->where(['msgStatus'=>0])->count();
            yii::$app->view->params['msg_count'] = $message_count ;//+ $exchange_count;
            yii::$app->view->params['platform'] = $config['platform']->CfgVal;
            $privilege = new Baseprivilege();
            $nodes = $privilege->getModules($this->roleId);
            yii::$app->view->params['nodes'] = $nodes;
        }
    }


    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function renderJson($params = array())
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return $params;
    }

    public function getParams($key)
    {
        $data = array_merge(yii::$app->request->post(),yii::$app->request->get());
        if (!empty($key)) {
            if (!empty($data[$key])) {
                return $data[$key];
            } else {
                return '';
            }
        } else {
            return $data;
        }
    }

    protected function renderJSONP($data=[], $msg ="ok", $code = 200)
    {
        header('Content-type: application/json');
        echo json_encode([
            "code" => $code,
            "msg"   =>  $msg,
            "data"  =>  $data,
            "req_id" =>  uniqid(),
        ]);
        return Yii::$app->end();
    }

    public function http_post($url, $data) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
}